import requests
import json
import re

def open_file_get_all_url(file_path):
    result_str_list = []
    # 打开文件
    with open(file_path, 'r', encoding='utf-8') as file:
        # 读取文件的所有行
        lines = file.readlines()
        # 遍历列表，逐行输出文件内容
        for line in lines:
            result_str_list.append(line.strip())
    return result_str_list

'''
读取生产URL返回全量URL LIST
'''
def open_file_filter_str_list():
    configObject = read_json_file("config/GetAllSwaggerURL.json")
    urlList = configObject["IntersectionFileList"]
    filtUrlList = []
    for path in urlList:
        filtUrlList.extend(open_file_get_all_url(path))
    return filtUrlList;

class UrlInfo:
    def __init__(self, url, name,hasDel):
        self.url = url
        self.name = name
        self.hasDel = hasDel
    
class SwaggerUi:
    def dealObject(self):
        for item in self.urlList:
            if self.baseUrl in item.url:
                continue
            else:
                item.url = "/"+self.baseUrl+item.url

    def __init__(self, baseUrl, urlList):
        self.baseUrl = baseUrl
        self.urlList = urlList

    def show(self):
        for item in self.urlList:
            print(str(self.baseUrl)+"|"+str(item.name)+"|"+str(item.url)+"|"+str(item.hasDel))
    
    def calcComplement(self,calcList):
        for item in self.urlList:
            if item.url in calcList:
                item.hasDel = "0"
            else:
                item.hasDel = "1"

        


'''
获取swaggerJSON数据
'''
def get_all_urls_from_swagger(swagger_url):
    # 发送请求获取Swagger JSON数据
    response = requests.get(swagger_url)
    swagger_data = response.json()
    
    # 提取所有路径
    urls = []
    paths = swagger_data.get('paths', {})
    
    for index,(key,value) in enumerate(paths.items()):
        if paths.get(key).get("post") is not None:
            urls.append(UrlInfo(key,paths[key]["post"]["summary"],"0"))
        elif paths.get(key).get("get") is not None:
            urls.append(UrlInfo(key,paths[key]["get"]["summary"],"0"))

    if len(urls) > 0:
        basePath = swagger_data.get('basePath', None)

        baseUrl = ""
        if basePath == None or basePath.strip() == "" or basePath == "/":
                baseUrl = (urls[len(urls)-1].url.split('/'))[1]
        else:
            baseUrl = (basePath.split('/'))[1]
        item =  SwaggerUi(baseUrl,urls)
        item.dealObject()
        return item
    return None

'''
读取JSON文件
'''
def read_json_file(file_path):
    # 打开并读取JSON文件
    with open(file_path, 'r', encoding='utf-8') as file:
        file_content = file.read()
    
    # 解析JSON文件内容
    data = json.loads(file_content)
    
    return data


if __name__ == "__main__":
    configObject = read_json_file("config/GetAllSwaggerURL.json")
    urlList = configObject["swaggerUrlList"]
    hasUseUrlList = open_file_filter_str_list()
    for item in urlList:
        swaggerUrl = get_all_urls_from_swagger(item)
        swaggerUrl.calcComplement(hasUseUrlList)
        swaggerUrl.show()